package com.souku.finance.application.adapter;

import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.souku.finance.application.R;
import com.souku.finance.application.bean.IndexBean;
import com.souku.finance.application.utils.UploadImageUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2016/5/13.
 */
public class MyImageAdapter extends PagerAdapter {

    private Activity context;
    private List<IndexBean.BannersBean> list;
    private OnImageViewClick listener;


    public MyImageAdapter(Activity context, List<IndexBean.BannersBean> list, OnImageViewClick listener) {
        this.context = context;
        this.list = list;
        this.listener=listener;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {

        View view = LayoutInflater.from(context).inflate(R.layout.item_cover, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.image_cover);

        if (list.size() != 0) {
            ImageLoader.getInstance().displayImage(list.get(position).getListImage(), imageView, UploadImageUtil.getSimpleOptions1());
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.click(list.get(position).getUrl());
                }
            });
        } else {
            imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.banner));
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.click(null);
                }
            });
        }
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return (view == object);
    }

    public interface OnImageViewClick{
        void click(String url);
    }
}
